package com.sl.au.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.sl.au.entity.Account;
import com.sl.au.entity.EventRegister;
import com.sl.au.entity.EventReport;
import com.sl.au.entity.EventToEvaluateUser;
import com.sl.au.entity.Goods;
import com.sl.au.entity.Option;
import com.sl.au.entity.User;
import com.sl.au.entity.EventProcessToEvaluateUser;
import com.sl.au.entity.VisitorReward;
import com.sl.au.entity.VisitorRewardProcessing;
import com.sl.au.service.EventRegisterService;
import com.sl.au.service.EventReportService;
import com.sl.au.service.EventToEvaluateUserService;
import com.sl.au.service.GoodsService;
import com.sl.au.service.OptionService;
import com.sl.au.service.UserEventTypeBindInfoService;
import com.sl.au.service.UserService;
import com.sl.au.service.VisitorRewardProcessingService;
import com.sl.au.service.VisitorRewardService;
import com.sl.au.service.EventProcessToEvaluateUserService;
import com.sl.utils.ComparatorUtil;
import com.sl.utils.ExdeUtil;
import com.sl.utils.SHA256Util;

/**
 * 
 * @author yangdi
 *
 */

@RestController
@RequestMapping(value = "/ui/exchange") 
public class ExchangeController  extends BaseController{
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private VisitorRewardProcessingService visitorRewardProcessingService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView,HttpServletRequest request) {
		String device = request.getParameter("device");
		String identify = request.getParameter("identify");
		if (StringUtils.hasText(device) && this.DEVICE.equals(device)) {
			modelAndView.setViewName("mobile/exchange/exchangeIndex");
			modelAndView.addObject("identify", identify);
			modelAndView.addObject("account", this.getMyAccount());
		}
		return modelAndView;
	}
	
	@RequestMapping(value = "getUser", method = RequestMethod.GET)
	public Map<String, Object> getUser() {
		Map<String, Object> map = new HashMap<String, Object>();
		double reward = 0;
		reward = visitorRewardProcessingService.getViewForVisitorRewardRepository().findOne(this.getMyAccount().getUser().getId()).getTotalRewardValue();
		map.put("contentTitle", "个人积分");
		map.put("contentNum", reward);
		return map;
	}

	@RequestMapping(value = "getGoodsPage/{page}", method = RequestMethod.GET)
	public Map<String, Object> getGoodsPage(@PathVariable("page") String page) {
		Map<String, Object> map = new HashMap<String, Object>();
		int pageNum = ExdeUtil.str2int(page);
		int start = pageNum * 5;
		Date date = ExdeUtil.getTodayEnd();
		List<Goods> goods = goodsService.getPageByDate(start, 5, date, "createTime", Sort.Direction.DESC);
		map.put("goods", goods);
		return map;
	}

	@RequestMapping(value = "exchangeDetail", method = RequestMethod.GET)
	public Map<String, Object> exchangeDetail() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<VisitorRewardProcessing> exchange = visitorRewardProcessingService.getRepository().getExchangeDetail(this.getMyAccount().getUser().getId());
		map.put("exchange", exchange);
		return map;
	}
	
	@RequestMapping(value = "getGoods/{id}", method = RequestMethod.GET)
	public Map<String, Object> getGoods(@PathVariable("id") String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		Goods goods = goodsService.getRepository().findOne(id);
		map.put("goods", goods);
		return map;
	}

	@RequestMapping(value = "exchangeGoods/{id}", method = RequestMethod.GET)
	public Map<String, Object> exchangeGoods(@PathVariable("id") String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		String msg = "";
		int code = 0;
		Goods goods = goodsService.getRepository().findOne(id);
		int goodsPrice = goods.getGoodsPrice();
		double reward = visitorRewardProcessingService.getViewForVisitorRewardRepository().findOne(this.getMyAccount().getUser().getId()).getTotalRewardValue();
		if(reward - goodsPrice >= 0) {
			VisitorRewardProcessing exchangeGoods = new VisitorRewardProcessing();
			exchangeGoods.setGoods(goods);
			exchangeGoods.setRewardValue(goodsPrice);
			exchangeGoods.setOperationTime(new Date());
			exchangeGoods.setDetails("游客积分兑换<"+goods.getGoodsName()+">");
			exchangeGoods.setOperator(this.getMyAccount().getUser());
			exchangeGoods.setUser(this.getMyAccount().getUser());
			exchangeGoods.setRedeemCode(exchangeGoods.getId());
			visitorRewardProcessingService.getRepository().save(exchangeGoods);
			msg = "商品兑换成功！\r\n您的兑换码:"+exchangeGoods.getId();
			code = 0;
		}else {
			msg = "积分不足，无法兑换商品！";
			code = 1;
		}
		map.put("msg", msg);
		map.put("code", code);
		return map;
	}
	
	
}
